/*
 * @Author: zy
 * @Date: 2019-07-26 15:28:45
 * @LastEditTime: 2021-07-14 18:05:37
 * @LastEditors: zengy
 * @Description: axios封装
 * @FilePath: \Mics3.0_3DViewer_WEB\3DViewer\src\utils\axios.js
 */

import axios from 'axios'
import store from '../store';
// import router from '../router';
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.baseURL = 'development' ? '/' : '/';

axios.create({
  headers: { BUSINESS_CODE: 'SWKSH' }
});

/**
 * @description: 请求拦截器
 * @param {*}
 * @return {*}
 */
axios.interceptors.request.use(
  function (config) {
    config.headers['Authorization'] = store.state.tokenType + " " +
      store.state.token
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

/**
 * @description: 响应拦截器
 * @param {*}
 * @return {*}
 */
axios.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)

/**
 * 返回失败提示函数
 * @method error
 * @param {Object} response 请求失败返回数据
 */
function error(response) {
  if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
    return response
  } else if (response.status === 202) {
    console.log('token过期');
    window.localStorage.clear()
  } else {
    console.error('数据获取错误')
  }
}
/**
 * 返回成功提示函数
 * @method success
 * @param {Object} response 请求成功返回数据
 */
function success(response) {
  return response
}

/**
 * axios封装
 * @method commonHttp
 * @param {String} method 请求方式
 * @param {String} url 请求url，除去baseURL
 * @param {Object} params 请求参数
 */
function commonHttp(method, url, params) {
  let httpConfig = {}
  if (method === 'GET' || method === 'DELETE') {
    httpConfig = {
      method: method,
      url: url,
      params: method === 'GET' || method === 'DELETE' ? params : null,
      timeout: 20000
    }
  } else {
    httpConfig = {
      method: method,
      url: url,
      data: method === 'POST' || method === 'PUT' ? params : null,
      timeout: 20000
    }
  }
  return new Promise((resolve, reject) => {
    axios(httpConfig)
      .then(res => {
        success(res)
        resolve(res.data)
      })
      .catch(response => {
        error(response)
        reject(response)
      })
  })
}

/**
 * 三种引入方式
 * 1. 全局引入
 * main.js app.use(axios);
 * 单页面使用import { getCurrentInstance} from 'vue'  
 * const { proxy } = getCurrentInstance(); 
 * proxy.getAxios()
 * 2.全局引入
 * main.js app.provide('axios', axios)
 * 单页面使用import { inject} from 'vue'
 * const axios = inject('axios');
 * axios.get()
 * 3. 普通引入，import xx from xx；引入的界面使用xx.get调用
 * 
 */
export default {
  install: function (app) {
    app.config.globalProperties.$getAxios = (url, params) => commonHttp('GET', url, params)
    app.config.globalProperties.$postAxios = (url, params) => commonHttp('POST', url, params)
    app.config.globalProperties.$putAxios = (url, params) => commonHttp('PUT', url, params)
    app.config.globalProperties.$delAxios = (url, params) => commonHttp('DELECT', url, params)
    app.config.globalProperties.__REQUEST_INTERVAL__ = 15 * 60 * 1e3; // 接口定时刷新时间间隔，需要手动设置定时器
  },
  get: (url, params) => commonHttp('GET', url, params),
  post: (url, params) => commonHttp('POST', url, params),
  put: (url, params) => commonHttp('PUT', url, params),
  del: (url, params) => commonHttp('DELECT', url, params)
}
